package org.itboys.mobile.service.mongo.mobile.information;

import org.apache.commons.lang.StringUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mdl.entity.mongo.information.MdlPortalImage;
import org.itboys.mdl.entity.mongo.information.MdlPortalInfo;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created on 2017/12/1.
 * @author 朱坤涛
 */
@Service
public class MdlPortalInfosService extends BaseMongoService<MdlPortalInfo>{

    @Autowired
    private MdlPortalImageService portalImageService;
    @Autowired
    private ResourceHolder resourceHolder;

    @Resource(name="mobileDs")
    private MongoDataSource dictDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return dictDataSource;
    }
    @Override
    protected Class<MdlPortalInfo> getEntityClass() {
        return MdlPortalInfo.class;
    }

    /**
     * 添加或修改资讯
     * @param info
     * @param list
     */
    public void savaInfo(MdlPortalInfo info, List<MdlPortalImage> list){
        if (info.getId()==01){
            info.setCreateTime(new Date());
            this.save(info);
            for (MdlPortalImage image:list){
                image.setPortalId(info.getId());
            }
            portalImageService.batchSave(list);
        }else {
            MdlPortalInfo mdlPortalInfo = this.getById(info.getId());
            mdlPortalInfo.setTitle(info.getTitle());
            mdlPortalInfo.setSummary(info.getSummary());
            mdlPortalInfo.setContext(info.getContext());
            mdlPortalInfo.setType(info.getType());
            mdlPortalInfo.setStatus(info.getStatus());
            if(StringUtils.isNotBlank(info.getZhuImage())) {
                mdlPortalInfo.setZhuImage(info.getZhuImage());
            }
            if(StringUtils.isNotBlank(info.getBigImage())) {
                mdlPortalInfo.setBigImage(info.getBigImage());
            }
            this.updateExceptEmpty(info.getId(), mdlPortalInfo);
            if(list != null && list.size() > 0) {
                for(MdlPortalImage image : list) {
                    image.setPortalId(info.getId());
                }
                portalImageService.batchSave(list);
            }
        }
    }

    public void deleteInfo(Long id) {
        MdlPortalInfo info = this.getById(id);
        if (info.getType() == 2) {
            Map<String, Object> param = new HashMap<>();
            param.put("portalId", id);
            portalImageService.delete(param);
        }
        this.delete(id);
    }

    /**
     * 轮播
     *
     * @param itemId
     * @return
     */
    public List<MdlPortalInfo> show(Long itemId) {
        String imgRoot = resourceHolder.getStringValue("imgRoot");
        Map<String, Object> map = new HashMap<>();
        map.put("itemId", itemId);
        map.put("status", 1);
        List<MdlPortalInfo> list = this.list(map);
        for (MdlPortalInfo info : list) {
            switch (info.getType()) {
                case 1:
                    info.setZhuImage(imgRoot + info.getZhuImage());
                    break;
                case 2:
                    Map<String,Object> param = new HashMap<>();
                    param.put("portalId",info.getId());
                    List<MdlPortalImage> images = portalImageService.list(param);
                    for (MdlPortalImage image : images) {
                        image.setImage(imgRoot + image.getImage());
                    }
                    info.setImages(images);
                    break;
                case 3:
                    info.setBigImage(imgRoot + info.getBigImage());
                    break;

                default:
                    break;
            }
        }
        return list;
    }

    /**
     * 资讯列表
     *
     * @param pageNo
     * @param pageSize
     * @param itemId
     * @return
     */
    public List<MdlPortalInfo> pagePortal(Integer pageNo, Integer pageSize, Long itemId, HttpServletRequest request) {
        String imgRoot = resourceHolder.getStringValue("imgRoot");
        Map<String, Object> map = new HashMap<>();
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        map.put("itemId", itemId);
        map.put("pageSize", pageSize);
        map.put("pageNo", pageNo);
        map.put("orderByKey", "-createTime");
        PageResult<MdlPortalInfo> page = this.pageQuery(request,map);
        for (MdlPortalInfo info : page.getData()) {
            switch (info.getType()) {
                case 1:
                    info.setZhuImage(imgRoot + info.getZhuImage());
                    break;
                case 2:
                    Map<String,Object> param = new HashMap<>();
                    param.put("portalId",info.getId());
                    List<MdlPortalImage> images = portalImageService.list(param);
                    for (MdlPortalImage image : images) {
                        image.setImage(imgRoot + image.getImage());
                    }
                    info.setImages(images);
                    break;
                case 3:
                    info.setBigImage(imgRoot + info.getBigImage());
                    break;

                default:
                    break;
            }
        }
        return page.getData();
    }
}
